
*------------------------------------------------------------------------------
*  TESS 2016 data
*==============================================================================

* ===========================================================
* combine data sets and fix some parts in TESS study
insheet using "${dir_raw}/TESS3_214_topic.csv", clear 
save "${dir_processed}/topic.dta", replace 

use "${dir_raw}/TESS3_214_Lee_Combined_Final_Data.dta",clear 
append using "${dir_raw}/TESS3_214_Lee_Extra_Interviews.dta", gen(add)
drop Q8 
rename *, lower
merge 1:1 caseid using "${dir_processed}/topic.dta", gen(m_topic)
save "${dir_processed}/tess_all.dta", replace

* fix the name-fix 	
use "${dir_processed}/tess_all.dta", clear 
keep caseid q6_name q7_*_name q10 
rename q6_name name1 
rename q7_1_name name2
rename q7_2_name name3
rename q7_3_name name4
rename q7_4_name name5
rename q7_5_name name6
rename q10 name7 
reshape long name@, i(caseid) j(order)

replace name = strtrim(name)
drop if name == ""

outsheet * using "${dir_processed}/tess_2016_name.csv", comma replace 

* here, I manually fix some names
insheet using "${dir_manual}/tess_2016_name_fix.csv", clear 
keep caseid order n_name 
drop if order == 7
reshape wide n_name, i(caseid) j(order)
egen n_names = rowtotal(n_name*)
save "${dir_processed}/tess_2016_name_fix.dta", replace 

* ==========================================================
* ----------- TESS 2016
* ==========================================================

use "${dir_processed}/tess_all.dta", clear 
merge 1:1 caseid using "${dir_processed}/tess_2016_name_fix.dta", gen(m_name)

gen id = caseid

* treatment condition 
gen random_pid_rep = dov_assign == 1 
recode xtess214 (1 2 5 6 =1) (else =0), gen(k_imp)
recode xtess214 (1 3 5 7 =1) (else =0), gen(k_prime)
recode xtess214 (5 6 7 8 =1) (else =0), gen(k_event)

* demographic variables 
gen c_age = ppage 
gen c_age4 = ppagect4 
	tab c_age4, gen(c_agec4)
gen c_age7 = ppagecat
	tab c_age7, gen(c_agec7)

gen c_female = ppgender == 2 if ~missing(ppgender)
recode ppethm (1=1) (2=2) (3/5=3), gen(c_race)
	gen c_white = c_race==1 if ~missing(c_race) 
	gen c_black = c_race==2 if ~missing(c_race) 
	gen c_others = c_race==3 if ~missing(c_race) 

tab ppethm, gen(c_race)

gen c_degree = ppeduc
recode ppeduc (1=0) (2=4) (3=6) (4=8) (5=9) (6=10) (7=11) (8=12) (9=12) (10=13) (11=14) (12=16) (13=18) (14=22), gen(c_educ)
gen c_degree_cat = ppeducat 
	gen c_edu_lthigh = c_degree_cat == 1 
	gen c_edu_high = c_degree_cat == 2 
	gen c_edu_college = c_degree_cat == 3 
	gen c_edu_bs = c_degree_cat == 4 

gen c_marital = ppmarit 
	gen c_married = c_marital == 1 | c_marital == 6 if ~missing(c_marital)
	gen c_single = c_marital == 5 if ~missing(c_marital)
	gen c_widowed = c_marital == 2 | c_marital == 3 | c_marital == 4 if ~missing(c_marital)

recode ppwork (1 2=1) (3 4 =2) (5=3) (6 7 =4), gen(c_wrkstat)
	gen c_working = c_wrkstat == 1 if ~missing(c_wrkstat)
	gen c_unemployed = c_wrkstat == 2 if ~missing(c_wrkstat)
	gen c_retired = c_wrkstat == 3 if ~missing(c_wrkstat)
	gen c_otherworks = c_wrkstat == 4 if ~missing(c_wrkstat)


gen c_hhead = pphhhead 
gen c_hsize = pphhsize
gen c_hincome = ppincimp 
gen c_hown = pprent == 1 
gen c_hnumber01 = ppt01 
gen c_hnumber25 = ppt25
gen c_hnumber612 = ppt612 
gen c_hnumber1317 = ppt1317 
gen c_hnumber18ov = ppt18ov 

egen c_hmember = rowtotal(c_hnumber*)
gen c_adults = c_hnumber18ov 
egen c_childs = rowtotal(c_hnumber01 c_hnumber25 c_hnumber612 c_hnumber1317)

gen c_metro = ppmsacat == 1 if ~missing(ppmsacat)

gen c_region4 = ppreg4
	gen c_region_ne = c_region4 == 1 if ~missing(c_region4)
	gen c_region_mw = c_region4 == 2 if ~missing(c_region4)
	gen c_region_so = c_region4 == 3 if ~missing(c_region4)
	gen c_region_we = c_region4 == 4 if ~missing(c_region4)
gen c_region9 = ppreg9
gen c_south = c_region4 == 3 if ~missing(c_region4)

*codebook dov_rel1 ,tab(20)
gen c_religion = dov_rel1 if dov_rel1 > 0 
	gen c_catholic = c_religion == 1 if ~missing(c_religion)
	gen c_protestant = c_religion == 2 if ~missing(c_religion)
	gen c_otherR = c_religion >=3 & c_religion <=12 if ~missing(c_religion)
	gen c_no_religion = c_religion == 13 if ~missing(c_religion)

gen c_attendance = 6-dov_rel2 if dov_rel2 >0 
recode dov_rel2 (-1 = .) (1 = 104) (2 =52) (3 = 24) (4=12) (5=1) (6=0), gen(c_attendance_day)

* instruments for political priming + pid / ideology scores
gen c_vote = (q1 == 1 | q1 == 2) if ~missing(q1) & q1 > 0 
gen c_pol_interest = 5 - q2 if q2 > 0 
gen c_pol_talk = 5 - q3 if ~missing(q3) & q3> 0 

* instruments -- searching the information about politics
gen ps_radio = q4_a
gen ps_tv = q4_b
gen ps_newspaper = q4_c
gen ps_megazine = q4_d
gen ps_gsearch = q4_e
gen ps_iblog = q4_f
gen ps_facebook = q4_g
gen ps_twitter = q4_h
gen ps_sns = q4_i
gen ps_other = q4_j

egen ps_nsearch = rowtotal(ps_*)
gen ps_none = q4_k 
replace ps_nsearch = . if q4_refused == 1
replace ps_none = . if q4_refused == 1

* XTESS214 / weight 
* ------------------ network data 
* network size 
egen n_size = anycount(q6 q7_1 q7_2 q7_3 q7_4 q7_5), values(1)
	replace n_size = 0 if q6 == 2 

gen n_size6 = n_size 
	replace n_size6 = 6.5 if n_size == 6

gen n_size7 = n_size 
	replace n_size7 = n_size + 1 if q9 == 7 

* corrected n_size !!
replace n_names = 0 if q6 == 2

gen n_order = q9 if q9 > 0 & ~missing(q9) 
gen why_noissue = q17 == 1 if q17>0 & ~missing(q17)
gen why_noperson = q17 == 2 if q17>0 & ~missing(q17)

* network characteristics 
gen n_name = dov_record
gen a0_female = q11 == 2 if ~missing(q11)
recode q12 (-1 = .) (4=1) (2=2) (1 3 5=3), gen(a0_race)
	gen a0_white = a0_race == 1 if ~missing(a0_race)
	gen a0_black = a0_race == 1 if ~missing(a0_race)
	gen a0_other = a0_race == 1 if ~missing(a0_race)

tab q13
gen a0_rel = q13 if q13 > 0 
tab a0_rel, gen(who)
	gen w_family = a0_rel >=1 & a0_rel <=5 if ~missing(a0_rel)
	gen w_friend = a0_rel == 7 if ~missing(a0_rel)
	gen w_cowork = a0_rel == 6 if ~missing(a0_rel)
	gen w_neighbor = a0_rel == 8 | a0_rel == 9 if ~missing(a0_rel)

gen a0_age = q15 
	replace a0_age = . if q15_codes== -1

* party identification-political ideology
gen c_ideo = ideo if ideo > 0 

gen c_partyid7 = .
	replace c_partyid7 = 1 if party1a == 1 & party3 == 1 
	replace c_partyid7 = 1 if party1b == 2 & party3 == 1 
	replace c_partyid7 = 2 if party1a == 1 & party3 == 2
	replace c_partyid7 = 2 if party1b == 2 & party3 == 2

	replace c_partyid7 = 3 if (party1a == 3 | party1a == 4) & party4 == 2
	replace c_partyid7 = 3 if (party1b == 3 | party1b == 4) & party4 == 2
	replace c_partyid7 = 4 if (party1a == 3 | party1a == 4) & party4 == 3
	replace c_partyid7 = 4 if (party1b == 3 | party1b == 4) & party4 == 3
	replace c_partyid7 = 5 if (party1a == 3 | party1a == 4) & party4 == 1
	replace c_partyid7 = 5 if (party1b == 3 | party1b == 4) & party4 == 1

	replace c_partyid7 = 6 if party1a == 2 & party2 == 2
	replace c_partyid7 = 6 if party1b == 1 & party2 == 2
	replace c_partyid7 = 7 if party1a == 2 & party2 == 1
	replace c_partyid7 = 7 if party1b == 1 & party2 == 1

* strength
gen c_party_int = abs(c_partyid7 - 4)
gen c_ideo_int = abs(c_ideo - 4)

* ---- partyid 7
gen a0_partyid7 = . 
replace a0_partyid7 = 1 if q16b == 2 & q16_add1 == 1
replace a0_partyid7 = 1 if q16a == 1 & q16_add1 == 1
replace a0_partyid7 = 2 if q16b == 2 & q16_add1 == 2
replace a0_partyid7 = 2 if q16a == 1 & q16_add1 == 2
replace a0_partyid7 = 3 if (q16b == 3 | q16b == 4 | q16b == 5) & q16_add3 == 2
replace a0_partyid7 = 4 if (q16b == 3 | q16b == 4 | q16b == 5) & q16_add3 == 3
replace a0_partyid7 = 5 if (q16b == 3 | q16b == 4 | q16b == 5) & q16_add3 == 1
replace a0_partyid7 = 3 if (q16a == 3 | q16a == 4 | q16a == 5) & q16_add3 == 2
replace a0_partyid7 = 4 if (q16a == 3 | q16a == 4 | q16a == 5) & q16_add3 == 3
replace a0_partyid7 = 5 if (q16a == 3 | q16a == 4 | q16a == 5) & q16_add3 == 1
replace a0_partyid7 = 6 if q16a == 2 & q16_add2 == 2
replace a0_partyid7 = 6 if q16b == 1 & q16_add2 == 2
replace a0_partyid7 = 7 if q16a == 2 & q16_add2 == 1
replace a0_partyid7 = 7 if q16b == 1 & q16_add2 == 1

** how to consider "5=don't know" / "-1=refused" in q16a and q16b 
replace a0_partyid7 = . if q16a == 5
replace a0_partyid7 = . if q16b == 5
replace a0_partyid7 = . if q16_add3 == -1
replace a0_partyid7 = . if q16_add1 == -1
replace a0_partyid7 = . if q16_add2 == -1

* four category 
gen c_partyid4 = . 
replace c_partyid4 = 1 if party1a == 1 | party1b == 2 // Democrat
replace c_partyid4 = 3 if (party1a == 3 | party1b == 3) // independent
replace c_partyid4 = 4 if (party1a == 4 | party1b == 4) // somethinge lse
replace c_partyid4 = 2 if party1a == 2 | party1b == 1 // Republican 

gen a0_partyid4 = . 
replace a0_partyid4 = 1 if q16a == 1 | q16b == 2 
replace a0_partyid4 = 3 if q16a == 3 | q16b == 3 
replace a0_partyid4 = 4 if q16a == 4 | q16b == 4 
replace a0_partyid4 = 2 if q16a == 2 | q16b == 1 

* three category; collapse "something else"
gen a0_partyid3 = . 
replace a0_partyid3 = 1 if q16a == 1 | q16b == 2
replace a0_partyid3 = 2 if q16a == 3 | q16a == 4 
replace a0_partyid3 = 2 if q16b == 3 | q16b == 4 
replace a0_partyid3 = 3 if q16a == 2 | q16b == 1

gen c_partyid3 = . 
replace c_partyid3 = 1 if party1a == 1 | party1b == 2
replace c_partyid3 = 2 if party1a == 3 | party1a == 4
replace c_partyid3 = 2 if party1b == 3 | party1b == 4
replace c_partyid3 = 3 if party1a == 2 | party1b == 1

* party uncertainty
tab q16a
tab q16b
tab q16_add1 
tab q16_add2
gen a0_partyid_dk = 0 if n_size > 0 
replace a0_partyid_dk = 1 if q16a == -1  | q16a == 5 
replace a0_partyid_dk = 1 if q16b == -1  | q16b == 5 
replace a0_partyid_dk = 1 if q16_add1 == -1  | q16_add2 == -1 | q16_add3 == -1

* party homophily 
gen a0_same_partyid3 = (a0_partyid3 == c_partyid3) if ~missing(a0_partyid3) & ~missing(c_partyid3)
gen a0_same_partyid4 = (a0_partyid4 == c_partyid4) if ~missing(a0_partyid4) & ~missing(c_partyid4)
gen a0_same_partyid7 = (a0_partyid7 == c_partyid7) if ~missing(a0_partyid7) & ~missing(c_partyid7)

* partisan homophily : continuous version
gen abs_diff_partyid = abs(c_partyid7 - a0_partyid7)
gen a0_party_homophily = (6-abs_diff_partyid)/6

* state 
*tab ppstaten
decode ppstaten, gen(state)

* length of discussion 
gen topic_nwords = wordcount(q8)
gen topic_length = strlen(q8)

replace topic_length = . if q8_refused == -1 
replace topic_nwords = . if q8_refused == -1 

* search topics 
gen topic_debate = . 
replace topic_debate = insert_q9_a_pre if ~missing(insert_q9_a_pre)
replace topic_debate = insert_q9_a_post if ~missing(insert_q9_a_post)
replace topic_debate = insert_q9_a if ~missing(insert_q9_a)

gen topic_election = . 
replace topic_election = insert_q9_b_pre if ~missing(insert_q9_b_pre)
replace topic_election = insert_q9_b_post if ~missing(insert_q9_b_post)
replace topic_election = insert_q9_b if ~missing(insert_q9_b)

gen topic_football = . 
replace topic_football = insert_q9_c_pre if ~missing(insert_q9_c_pre)
replace topic_football = insert_q9_e_post if ~missing(insert_q9_e_post)

gen topic1_genewilder = insert_q9_d_pre
gen topic1_spacex = insert_q9_e_pre
gen topic1_chrisbrown = insert_q9_g_pre
gen topic1_earthquake = insert_q9_h_pre 
gen topic1_hurricane = insert_q9_i_pre 

gen topic2_shawtylo = insert_q9_d_post 
gen topic2_genniferflowers = insert_q9_f_post 
gen topic2_arnoldpalmer = insert_q9_g_post 
gen topic2_bradpitt = insert_q9_h_post 

gen topic_none = insert_q9_j_pre 
replace topic_none = insert_q9_j if ~missing(insert_q9_j)
replace topic_none = insert_q9_j_post if ~missing(insert_q9_j_post)

gen dataset="TESS"
gen year = 2016

drop q8 q10 

foreach var of varlist c_* {
	local newname = substr("`var'", 3, .)
   	rename `var' r_`newname'
}

drop q1-ppwork insert_q9_a-n_name6 ps_radio-ps_none 

saveold "${dir_processed}/tess_2016.dta", replace version(12)



